package com.jozen.smart_toilet_system.tagView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.jozen.smart_toilet_system.R;
import com.jozen.smart_toilet_system.bean.BathroomParameter;
import com.ruffian.library.widget.REditText;

@SuppressLint("WrongConstant")
public class TagConfigPopup extends PopupWindow implements View.OnClickListener {
    private Context mContext;
    private View root;
    private REditText et_sn_code;
    private CheckBox cb_checked;
    private TagViewCallBack callBack;
    private boolean isBtnClick;//是否点击确认、或者清空按钮
    private boolean isSos;//是否是第三卫生间紧急按钮

    public TagConfigPopup(Context context) {
        this.mContext = context;
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setTouchable(true);
        setOutsideTouchable(true);
        setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setFocusable(true);
        root = LayoutInflater.from(mContext).inflate(R.layout.popu_tag_config_popup, null);
        setContentView(root);
        initView();
    }

    public void setCallBack(TagViewCallBack tokenCallBack) {
        callBack = tokenCallBack;
    }

    public void setOldSnCode(String snCode, boolean flag) {
        if (!TextUtils.isEmpty(snCode)) {
            et_sn_code.setText(snCode);
        } else {
            et_sn_code.setText("");
        }

        if (flag) {
            cb_checked.setChecked(true);
        } else {
            cb_checked.setChecked(false);
        }
    }


    private void initView() {
        et_sn_code = root.findViewById(R.id.et_sn_code);
        cb_checked = root.findViewById(R.id.cb_checked);
        root.findViewById(R.id.btn_ok).setOnClickListener(this);
        root.findViewById(R.id.btn_clear).setOnClickListener(this);
        setOnDismissListener(() -> {
            if (isBtnClick) {
                isBtnClick = false;
            } else {
                if (callBack != null) {
                    cb_checked.setChecked(false);
                    callBack.call(-1, null);
                }
            }

        });

        cb_checked.setOnCheckedChangeListener((buttonView, isChecked) -> {
            isSos = isChecked;
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ok:
                if (callBack != null) {
                    isBtnClick = true;
                    if (!TextUtils.isEmpty(et_sn_code.getText().toString().trim())) {
                        BathroomParameter parameter = new BathroomParameter();
                        parameter.setSnCode(et_sn_code.getText().toString().trim());
                        parameter.setSos(isSos);
                        callBack.call(2, parameter);
                        cb_checked.setChecked(false);
                        et_sn_code.setText("");
                    } else {
                        Toast.makeText(mContext, "请输入探测器SN码！", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            case R.id.btn_clear:
                if (callBack != null) {//删除当前配置信息
                    callBack.call(1, true);
                    et_sn_code.setText("");
                }
                break;
        }
    }
}
